package com.sunshine.androidjavapractise.reflect;

import android.content.Context;
import android.content.pm.PackageManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class reflectClass {
    public void callOtherAppStaticMethod(Context context) {
        try {
            Context otherPackageContext = context.createPackageContext("otherPackageName", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
            if (null != otherPackageContext) {
                Class clazz = otherPackageContext.getClassLoader().loadClass("otherPackageClassName");
                if (null != clazz) {
                    Method method = clazz.getDeclaredMethod("methodName", null);// second param class type
                    method.invoke(null, "arg");
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
